﻿using System;
using System.ServiceModel;
using TeamTasker.Contracts;
using TeamTasker.Utils;

namespace TeamTasker.Server.Service
{
    public class DefaultServiceSessionManager : IServiceSessionManager
    {
        public void CreateSession(Session session)
        {
            IContextChannel channel = OperationContext.Current.Channel;
            ClientSession clientSession = channel.Extensions.Find<ClientSession>();
            if (clientSession == null) // ok, no session for this user
            {
                clientSession = new ClientSession();
                clientSession.ID = session.ID;
                clientSession.UserID = session.UserID;
                clientSession.StartTime = session.StartTime;

                /*
                ITeamTaskerClientCallback callback =
                    OperationContext.Current.GetCallbackChannel<ITeamTaskerClientCallback>();
                Validate.NotNull(callback, "Client callback object is NULL in service operation context");
                clientSession.ClientCallback = callback;
                */

                channel.Extensions.Add(clientSession);
                Log.Info("Session is created for user ID " + clientSession.UserID);
            }
            else
            {
                // TODO: think if need to raise an exception
            }
        }

        public ClientSession GetSession()
        {
            IContextChannel channel = OperationContext.Current.Channel;
            ClientSession clientSession = channel.Extensions.Find<ClientSession>();
            return clientSession;
        }
    }
}
